﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SPAdvancedSearch.Utility
{
    class QueryStringParser
    {


        #region Constants

        private const string REG_EX = "(?<Property>\\w+):(?<Value>\\w+|\"[^\"\\r\\n]*\")\\s*";

        #endregion

        #region Fields

        private MatchCollection _userEnteredMatches;
        private Regex _regEx;

        #endregion

        #region Ctor

        /// <summary>
        /// Creates an instance of <see cref="QueryStringParser"/>.
        /// </summary>
        /// <param name="queryString"></param>
        public QueryStringParser(string queryString)
        {
            if (!string.IsNullOrEmpty(queryString))
            {
                _regEx = new Regex(REG_EX);
                _userEnteredMatches = _regEx.Matches(queryString);
            }
        }

        #endregion

        #region Properties

        #endregion

        #region Methods

        /// <summary>
        /// Returns the user-entered keyword value from the querystring.
        /// </summary>
        /// <param name="propName"></param>
        /// <returns></returns>
        public string ParseValueFromQueryString(string propName)
        {
            if (_userEnteredMatches == null)
                return string.Empty;

            foreach (Match m in _userEnteredMatches)
            {
                if (m.Groups["Property"].Value == propName)
                {
                    //TODO: verify that removing quotes is really valid..
                    return m.Groups["Value"].Value.Replace("\"", string.Empty);
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Determines if the <see cref="SearchProperty"/> managed property exists in the current querystring.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool ContainsAdvancedSearchProperties(List<SearchProperty> list)
        {
            //HttpContext.Current.Response.Write("checking: " + list.Count);
            if (_userEnteredMatches == null)
                return false;

            foreach (Match m in _userEnteredMatches)
            {
                string managedProperty = m.Groups["Property"].Value;
                //HttpContext.Current.Response.Write("checking: " + managedProperty);
                SearchProperty prop = (from p in list where p.ManagedPropertyName == managedProperty select p).FirstOrDefault();
                if (prop != null)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Removes the advanced search properties from the given searchText.
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public string RemoveAdvancedSearchProperties(string searchText)
        {
            return _regEx.Replace(searchText, string.Empty);
        }

        #endregion

    }
}
